﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Common;

public partial class system_documentcategoryeditor : PageBase
{
    public string PageTitle = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "getcategorylist":
                LoadParentCategoryList();
                break;
        }

        ///保存
        if (RequestBase.GetFormString("__EVENTTARGET") == "save")
        {
            SaveInfo();
        }

        if (!IsPostBack)
        {
            if (RequestBase.GetQueryString("otype") == "modify")
            {
                PageTitle = "修改目录信息";
                Initialize();
            }
            else
            {
                PageTitle = "新增目录信息";
                selDocumentType.Value = RequestBase.GetQueryString("typecode");
                txtCategoryId.Value = RequestBase.GetQueryString("parentid");
            }
        }
    }

    #region 类别列表数据绑定
    protected StringBuilder OPTIONS = null;
    /// <summary>
    /// 获取子类明细
    /// </summary>
    void LoadParentCategoryList()
    {
        string categoryid = RequestBase.GetFormString("categoryid");
        OPTIONS = new StringBuilder();

        DataTable dat = BLL.CommonBLL.Select("DocumentCategoryBase", "*", " AND DocumentTypeCode = " + categoryid + "  ORDER BY LineNum ASC");
        OPTIONS.Append(string.Format("<option value=\"{0}\">{1}</option>", "00000000-0000-0000-0000-000000000000", "-- 顶级类目"));
        DropdownlistDataBind(1, "00000000-0000-0000-0000-000000000000", dat);
        dat.Dispose();

        BLL.AjaxHelper.ResponseWrite(OPTIONS.ToString());
    }

    /// <summary>
    /// 类别列表邦定
    /// </summary>
    /// <param name="intLayer">级别</param>
    /// <param name="parentId">父目录ID</param>
    /// <param name="dt">目录数据表</param>
    void DropdownlistDataBind(int intLayer, string parentId, DataTable dt)
    {
        if (dt == null)
            return;

        DataRow[] dr = dt.Select("Layer = " + intLayer.ToString() + " AND ParentId = '" + parentId + "'");

        ArrayList al = new ArrayList(); 

        string str = ""; int intCategoryLayerID = 0;
        foreach (DataRow d in dr)
        {
            str = "　| ";
            intCategoryLayerID = int.Parse(d["Layer"].ToString());
            if (intLayer == 1)
                str = " + ";
            for (int i = 1; i < intLayer * intLayer; i++)
                str += "--";

            OPTIONS.Append(string.Format("<option value=\"{0}\">{1}</option>", d["CategoryId"].ToString(), "\t" + str + "\t" + d["CategoryName"].ToString()));
            DropdownlistDataBind(intCategoryLayerID + 1, d["CategoryId"].ToString().Trim(), dt);
        }
    }
    #endregion

    #region 信息初始化
    void Initialize()
    {
        GetObj = BLL.DocumentCategoryBase.GetObjById(RequestBase.GetQueryString("categoryid"));
    }

    ///// <summary>
    ///// 信息实体
    ///// </summary>
    Model.DocumentCategoryBase GetObj
    {
        get
        {
            Model.DocumentCategoryBase _cat = new Model.DocumentCategoryBase();

            if (!string.IsNullOrEmpty(RequestBase.GetQueryString("categoryid").Trim()))
            {
                _cat.CategoryId = RequestBase.GetQueryString("categoryid").Trim();
            }

            _cat.CategoryName = txtCategoryName.Value.Trim();
            _cat.DocumentNumberPrefix = txtDocumentNumberPrefix.Value.Trim();
            _cat.DocumentTypeCode = int.Parse(selDocumentType.Value.Trim());
            _cat.LineNum = int.Parse(txtLineNum.Value.Trim());
            _cat.ParentId = txtCategoryId.Value.Trim();

            return _cat;
        }
        set
        {
            if (value != null)
            {
                txtCategoryName.Value = value.CategoryName;
                txtDocumentNumberPrefix.Value = value.DocumentNumberPrefix;
                selDocumentType.Value = value.DocumentTypeCode.ToString();
                txtLineNum.Value = value.LineNum.ToString();
                txtCategoryId.Value = value.ParentId.Trim();
                txtOrgCategoryId.Value = value.ParentId.Trim();
            }
        }
    }
    #endregion

    void SaveInfo()
    {
        string res = "";

        if (RequestBase.GetQueryString("otype").Trim() == "add")
        {

            res = BLL.DocumentCategoryBase.Add(GetObj);
        }

        if (RequestBase.GetQueryString("otype").Trim() == "modify")
        {
            if (txtOrgCategoryId.Value.Trim() == txtCategoryId.Value.Trim())
                res = BLL.DocumentCategoryBase.Modify(GetObj, 0);
            else
                res = BLL.DocumentCategoryBase.Modify(GetObj, 1);
        }

        switch (res)
        {
            case "succeeded":
                if (RequestBase.GetQueryString("otype") == "add")
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("目录添加成功!", "window.opener.document.getElementById(\"RefreshItems\").click(); window.close();");
                    else
                        JScript.AlertAndRedirect("目录添加成功!", "../system/documentcategoryeditor.aspx?otype=add&parentid=" + txtCategoryId.Value.Trim() + "&typecode=" + selDocumentType.Value.Trim());
                }
                else
                {
                    if (RequestBase.GetFormString("__EVENTARGUMENT") == "saveandclose")
                        JScript.AlertAndReturnValueToParent("目录信息修改成功!", "window.opener.document.getElementById(\"RefreshItems\").click(); window.close();");
                    else
                        JScript.AlertAndRedirect("目录添加成功!", "../system/documentcategoryeditor.aspx?otype=add&parentid=" + txtCategoryId.Value.Trim() + "&typecode=" + selDocumentType.Value.Trim());
                }
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }
}
